<?php

class status_StatusesController extends Zend_Controller_Action
{
  function init() {
  	header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
	header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
    $this->getHelper('viewRenderer')->setNoRender();
  }


  //fake Action just to test the creation of the index

  function createluceneindexAction() {
  	require_once('Myzen/Search/Lucene.php');

	$index = Myzen_Search_Lucene::create('statuses');
  }


  //creation of a new status for the user
  function createAction() {

	require_once('status/objects/status.php');
    try{
	  $id= Status_Object_Status::create($this->_getParam('status'));
    } catch(Status_Object_Exception $e) {
      $data['status'] = 	'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage();
      echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;
    }
	$data['status'] = 	'OK';
	
	//We recreate the status
	$status = new Status_Object_Status($id);
	
	//We tru publishing the status to user streams
    require_once('status/objects/timeline.php'); 
	Status_Object_Timeline::publishStatusToUser($status);
	
	//If the use is asynchronous, we put a message in a queue
	$config = Zend_Registry::get('dt_config');
	if($config->asynchronous->use) {
		Zend_Registry::get('dt_log')->log('Pushing status in asynchronous mode, id:'.$id,Zend_Log::INFO);
		$queue = Zend_Registry::get('dt_asynchronousQueue');		
		$queue->send(new Myzen_Queue_AsynchronousJobMessage('status','statuses','indexing',array('statusId'=>$id)));
	} else { //Else we add the indexing in the stack
		Zend_Registry::get('dt_log')->log('Status is indexed in synchronous mode, id:'.$id,Zend_Log::INFO);		
		$this->_helper->actionStack('indexing', 'statuses','status', array('statusId'=>$id));
	}
	
	//we can t use status as it is alredy used to say evrything went fine
	$data['data'] =  $status->asArray();	
	
	echo Zend_Registry::get('dt_serializer')->serialize($data);
    return;
  } //end create action


  //read a particular status (only works when the status is public)
  function readAction() {
    require_once('status/objects/status.php');
    try{
	  $status = new Status_Object_Status($this->_getParam('id'));
    } catch(Status_Object_Exception $e) {
      $data['status'] = 	'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage();
      echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;
    }
	
	//we check if the status is visible by the user
	if(!$status->isVisible()) {
      $data['status'] = 	'KO';
      $data['error']['num'] = 	8;
      $data['error']['message'] = 	'Invalid identifier';
      echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;		
	}
	
	$data['status'] = 	'OK';
	//we can t use status as it is alredy used to say evrything went fine
	$data['data'] =  $status->asArray();	
	
	echo Zend_Registry::get('dt_serializer')->serialize($data);
    return;
  } //end read action
  
  function deleteAction() {
    require_once('status/objects/status.php');
    try{
		$status = new Status_Object_Status($this->_getParam('id'));
		Zend_Registry::get('dt_log')->log('deleting status '.$this->_getParam('id'),Zend_Log::INFO);
		//we start by deleting the status object
		$n = $status->delete();
		if(1 != $n) Zend_Registry::get('dt_log')->log('Important issue while deleting status '.$status->getId().'. '.$n.' statuses deleted', Zend_Log::CRIT);
		//then we delete the status from the timelines
		require_once('status/objects/timeline.php');
		$n = Status_Object_Timeline::deleteStatus($status);	
		Zend_Registry::get('dt_log')->log($n.' occurences of the status deleted from the stream database',Zend_Log::INFO);

		require_once('status/objects/tagsCounter.php');
		Status_Object_TagsCounter::deleteStatus($status);
	  
    } catch(Status_Object_Exception $e) {
      $data['status'] = 	'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage();
      echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;
    }
    
    //No issue, we just return ok
    $data['status'] = 	'OK';    
    echo Zend_Registry::get('dt_serializer')->serialize($data);	
    return;    
    
  }
  
	function indexingAction() {
		Zend_Registry::get('dt_log')->log('indexing new status:'.$this->_getParam('statusId'),Zend_Log::INFO);
		require_once('status/objects/status.php');
		require_once('status/objects/timeline.php');
		$status = new Status_Object_Status($this->_getParam('statusId'));
		
		//we publish the stream to follower homes (better if handled asynchonously)
		Status_Object_Timeline::publishStatusToFollowers($status);
		
		//We store the status in the lucene index
		try{
			$status->publishToLucene();
		} catch(Exception $e) {
			Zend_Registry::get('dt_log')->log('issue indexing status to lucene for status '.$status->getId().':'.$e->getMessage(),Zend_Log::ERR);				
		}
		
		//we publish the status to the mentions if any
		$mentions = $status->getMentions();
		if(!empty($mentions)) {
			Status_Object_Timeline::publishStatusToMentions($status);		
		}
		//we publish the status to the tags if any
		$tags = $status->getTags();
		if(!empty($tags)) {
			Status_Object_Timeline::publishStatusToTags($status);
			
			Zend_Registry::get('dt_log')->log('publishing status to tagsCounter',Zend_Log::INFO);
			require_once('status/objects/tagsCounter.php');
			Status_Object_TagsCounter::publishStatus($status);	

			Zend_Registry::get('dt_log')->log('publishing status to listeners',Zend_Log::INFO);		
			require_once('status/objects/listener.php');
			$listenersList = Status_Object_Listener::getListeners($status);
			Zend_Registry::get('dt_log')->log(serialize($listenersList),Zend_Log::DEBUG);		
			
			if(!empty($listenersList)) {
				$asynchronousUse = Zend_Registry::get('dt_config')->asynchronous->use;
				if($asynchronousUse) $queue = Zend_Registry::get('dt_asynchronousQueue');
				foreach($listenersList as $num => $oListener) {
					Zend_Registry::get('dt_log')->log('publishing status to listener '.$oListener->getModule().'-'.$oListener->getController().'-'.$oListener->getAction(),Zend_Log::INFO);	
					if($asynchronousUse) {
						$queue->send(new Myzen_Queue_AsynchronousJobMessage(
							$oListener->getModule(),
							$oListener->getController(),
							$oListener->getAction(),
							array('status'=> $status->asArray())
							)
						);
					} //end asynchronous
					else {
						$this->_helper->actionStack(
							$oListener->getAction(), 
							$oListener->getController(),
							$oListener->getModule(), 
							array('status'=> $status->asArray())
						);
					} //end not asynchronous
				}
			
			
			
			} //not empty listenersList
			
		} //not empty $tags
	} //end function indexing
  
} //End class

